# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/21 22:16
# @Author   : Robot-Zsj
"""
description:

i:
1,4,8,7

o:
41
"""

nums = list(map(int, input().split(",")))
mapping = {2: 5, 5: 2, 6: 9, 9: 6}


def dfs(vis, path, res):
    if len(path) > 0:
        res.append(int("".join(map(str, path))))

    if len(path) == len(nums):
        return

    for i in range(len(nums)):
        if vis[i]:
            continue

        vis[i] = True

        path.append(nums[i])
        dfs(vis, path, res)
        path.pop()

        if nums[i] in mapping:
            path.append(mapping[nums[i]])
            dfs(vis, path, res)
            path.pop()

        vis[i] = False


def solution():
    for num in nums:
        if num < 1 or num > 9:
            return -1

    setNums = set(nums)

    if len(setNums) != 4:
        return -1

    if 2 in setNums and 5 in setNums:
        return -1

    if 6 in setNums and 9 in setNums:
        return -1

    vis = [False] * len(nums)
    path = []
    res = []

    dfs(vis, path, res)

    res.sort()
    n = min(max(nums), len(res))
    return res[n - 1]


print(solution())